package com.mj.haier.calc.sharding;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

public class PreciseSettlementCycleShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    private final Logger log = LoggerFactory.getLogger(PreciseTradeShardingAlgorithm.class);

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
        log.debug("availableTargetNames: {}", JSON.toJSONString(availableTargetNames));
        log.debug("shardingValue: {}", JSON.toJSONString(shardingValue.getValue()));
        List<String> shardingResults = new ArrayList<>();
        shardingResults.add(0, "az_eorder_settlement_process_default");
        availableTargetNames.forEach(targetName -> {

            String suffix = targetName.substring(targetName.lastIndexOf("_") + 1);
            if(StringUtils.isNoneBlank(suffix)
                && suffix.equals(shardingValue.getValue())){
                shardingResults.add(0, targetName);
            }
        });
        return shardingResults.get(0);
    }

}